<template>
  <div class="app-container">
    <!-- 条件搜索 -->
    <div class="filter-container">
      <el-form ref="queryForm" :model="listQuery.param" size="small" :inline="true" label-width="auto">
        <el-form-item label="任务名称" prop="jobName">
          <el-input v-model="listQuery.param.jobName" placeholder="任务名称" class="filter-item" @keyup.enter.native="handleFilterQuery" />
        </el-form-item>
        <el-form-item label="任务分组" prop="jobGroup">
          <el-input v-model="listQuery.param.jobGroup" placeholder="任务分组" class="filter-item" @keyup.enter.native="handleFilterQuery" />
        </el-form-item>
        <el-form-item label="任务描述" prop="jobDesc">
          <el-input v-model="listQuery.param.jobDesc" placeholder="任务描述" class="filter-item" @keyup.enter.native="handleFilterQuery" />
        </el-form-item>
        <el-form-item label="执行结果" prop="jobStatus">
          <el-select v-model="listQuery.param.jobStatus" placeholder="执行结果" style="width: 207px;" class="filter-item" @change="handleFilterQuery">
            <el-option key="RUNNING" label="正在执行" value="RUNNING" />
            <el-option key="SUCCESS" label="执行成功" value="SUCCESS" />
            <el-option key="FAIL" label="执行失败" value="FAIL" />
          </el-select>
        </el-form-item>
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilterQuery">
          搜索
        </el-button>
        <el-button v-waves class="filter-item" icon="el-icon-refresh-right" @click="handleRestQuery">
          重置
        </el-button>
      </el-form>
    </div>
    <div class="filter-container">
      <div style="float: right;">
        <el-button icon="el-icon-refresh" circle @click="handleFilterQuery" />
      </div>
    </div>

    <!-- 数据表格 -->
    <el-table
      :key="tableKey"
      v-loading="loading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      scrollbar-always-on="true"
    >
      <el-table-column type="selection" width="50px" align="center" />
      <el-table-column label="序号" type="index" width="50px" align="center" />

      <el-table-column label="流水号" min-width="140px" align="left">
        <template v-slot="{row}">
          <span>{{ row.serialNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属服务" min-width="100px" align="left">
        <template v-slot="{row}">
          <span>{{ row.serviceName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务分组" min-width="100px" align="left">
        <template v-slot="{row}">
          <span>{{ row.jobGroup }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务名称" min-width="100px" align="left">
        <template v-slot="{row}">
          <span>{{ row.jobName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务描述" min-width="100px" align="left">
        <template v-slot="{row}">
          <span>{{ row.jobDesc }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行节点" min-width="110px" align="left">
        <template v-slot="{row}">
          <span>{{ row.jobHost }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行结果" min-width="90px" align="left">
        <template v-slot="{row}">
          <el-tag v-if="row.jobStatus === 'RUNNING'">正在执行</el-tag>
          <el-tag v-else-if="row.jobStatus === 'SUCCESS'" type="success">执行成功</el-tag>
          <el-tag v-else type="danger">执行失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="耗时" min-width="85px" align="left">
        <template v-slot="{row}">
          <span>{{ row.duration }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结果描述" min-width="150px" align="left">
        <template v-slot="{row}">
          <span>{{ row.jobMsg }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" width="160px" align="left">
        <template v-slot="{row}">
          <span>{{ row.startTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行时间" width="160px" align="left">
        <template v-slot="{row}">
          <span>{{ row.fireTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" width="160px" align="left">
        <template v-slot="{row}">
          <span>{{ row.endTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="90px" class-name="small-padding fixed-width">
        <template v-slot="{row,$index}">
          <el-button size="mini" type="info" icon="el-icon-document" @click="handleDetail(row, $index)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 表格分页 -->
    <pagination :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="handleInit" />

    <!--step日志页面-->
    <step-log ref="stepLog" :step-log-data-params="step" />

    <!-- 详情日志页面 -->
    <el-drawer
      :visible.sync="visible"
      direction="rtl"
      size="50%"
      :close-on-press-escape="false"
      :wrapper-closable="false"
      :show-close="false"
    >
      <template v-slot:title>
        <div class="drawer-header">
          <span>错误日志</span>
          <div>
            <el-button icon="el-icon-close" @click="handleClose">
              取消
            </el-button>
          </div>
        </div>
      </template>
      <editor :value="jobTrace" :options="editorOptions" lang="java" theme="chrome" width="100%" height="100%" @init="editorInit" />
    </el-drawer>
  </div>
</template>

<script>
import { handlePageList } from './api'
import permission from '@/directive/permission' // 权限判断指令
// import checkPermission from '@/utils/permission' // 权限判断函数
import waves from '@/directive/waves' // waves directive
// import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination/index.vue'
import StepLog from '@/views/tools/task/log/detail.vue'

export default {
  // 当前页面名称（组件）
  name: 'TkJobLogForm',
  // 引用组件
  components: {
    StepLog,
    Pagination,
    editor: require('vue2-ace-editor')
  },
  directives: { permission, waves },
  filters: {},
  data() {
    return {
      // 列表table的key
      tableKey: 0,
      // 遮罩层
      loading: true,
      // table数据对象
      list: [],
      // 总条数
      total: 0,
      // table条件查询对象
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        // sort: '+id',
        param: {
          // 按需调整
          jobName: undefined,
          jobGroup: undefined,
          jobDesc: undefined,
          jobStatus: undefined
        }
      },
      // 表单数据对象
      formData: {},
      // 是否显示弹出层
      visible: false,
      // 弹出层状态值（新增和修改共用同一个表单页面）
      state: '',
      // 弹出层标题
      title: {
        update: '修改',
        create: '新增',
        detail: '详情'
      },
      // 权限标识
      permission: {
        // 按需补充
        add: ['xx:add'],
        edit: ['xx:edit'],
        del: ['xx:del']
      },
      // 步骤日志
      step: {
        serialNo: undefined,
        visible: false
      },
      // 配置项
      editorOptions: {
        wrap: true, // 换行
        enableLiveAutocompletion: true, // 智能补全
        enableSnippets: true, // 启用代码段
        readOnly: true
      },
      // 展示的错误信息
      jobTrace: ''
    }
  },
  computed: {},
  watch: {},
  created() {
    this.handleInit()
  },
  mounted() {},
  methods: {
    // 查询table数据对象
    handleInit() {
      this.loading = true
      handlePageList(this.listQuery).then(res => {
        this.list = res.data
        if (res.total) {
          this.total = Number(res.total)
        }
        this.loading = false
      })
    },
    // 初始化编辑器
    editorInit() {
      require('brace/ext/language_tools')
      require('brace/mode/html')
      require('brace/mode/javascript')
      require('brace/mode/less')
      require('brace/mode/java')
      require('brace/theme/chrome')
      require('brace/snippets/javascript')
      require('brace/snippets/java')
    },
    // 条件查询table数据
    handleFilterQuery() {
      this.listQuery.pageNum = 1
      this.handleInit()
    },
    // 重置table查询条件
    handleRestQuery() {
      this.$refs.queryForm.resetFields()
    },
    // 详情
    handleDetail(row) {
      if (row.jobType === 'BATCH') {
        this.step.serialNo = row.serialNo
        this.step.visible = true
        // 由于子组件是异步的，所以需要调用nextTick
        this.$nextTick(() => {
          // 调用子组件的方法
          this.$refs['stepLog'].handleInit()
        })
      } else {
        this.handleTrace({ visible: true, jobTrace: row.jobTrace })
      }
    },
    // 查询错误日志
    handleTrace(param) {
      this.visible = param.visible
      this.jobTrace = param.jobTrace
    },
    // 关闭弹出层
    handleClose() {
      this.visible = false
      this.jobTrace = ''
    }
  }
}
</script>
